package future;

import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestFuture {

    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(2);
        ArrayList<Future<Integer>> futureList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            int val = i;
            Future<Integer> future = service.submit(() -> val);
            futureList.add(future);
        }
        service.shutdown();
        for (Future<Integer> f : futureList){
            if (f.isDone()){
                try {
                    System.out.println("值："+ f.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                    System.out.println("出现异常");
                }
            }
        }
    }
}
